/*
 * Copyright (c) 2007 Compl Yue Still, All Rights Reserved.
 */

package hbi.wire.as3.host
{
	import flash.utils.IDataInput;
	import flash.utils.ByteArray;

	public class TypeConstant implements IncomingTag
	{
		private var hostEnd:HostEnd;
		
		public function TypeConstant(hostEnd:HostEnd)
		{
			this.hostEnd = hostEnd;
		}
		
		private var len:int = -1;
		private var buf:ByteArray;

		public function processIncomingData(input:IDataInput):Boolean
		{
			if(len < 0)
			{
				if(input.bytesAvailable < 2)
				// length not available yet
					return false;
				len = input.readUnsignedShort();
				buf = new ByteArray();
			}
			
			var len2Read:int = Math.min(len-buf.length, input.bytesAvailable);
			if(len2Read > 0)
			{
				input.readBytes(buf, buf.length, len2Read);
				if(buf.length >= len)
				{
					var typeName:String = buf.readUTFBytes(len);
					hostEnd.resolveType(typeName);
					return true;
				}
			}
			
			return false;
		}
		
	}
}
